﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;

namespace LiWei
{
    public partial class 总线运动 : Form
    {
        public 总线运动()
        {
            InitializeComponent();
        }
        int[] axislist= { 0};
        string strFileName = "";

        //打开选择Bas总线文件
        private void OpenCanFile_Click_1(object sender, EventArgs e)
        {
            if (OpenCanFile.Text == "打开并连接")
            {
                #region 打开文件
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Bas文件(*.bas)|*.bas|所有文件|*.*";
                ofd.ValidateNames = true;   //验证用户输入的是否是一个有效的Windows文件名
                ofd.CheckFileExists = true; //验证路径的有效性
                ofd.CheckPathExists = true;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    strFileName = ofd.FileName;
                }
                #endregion

                #region 连接总线
                var ret = PMC_BasDown(Data.handle, strFileName, 1);
                OpenCanFile.Text = "连接中";
                OpenCanFile.Enabled = false;
                //System.Threading.Thread.Sleep(1000);

                if (ret != 0)
                {
                    OpenCanFile.Text = "打开并连接";
                    OpenCanFile.Enabled = true;
                    MessageBox.Show("总线连接失败");
                }
                else
                {
                    int[] connectflag = { 0 };
                    int num = 0;
                    for (int i = 0; i < 10; i++)
                    {
                        PMC_GetVariableInt(Data.handle, "connectFlag", 2, connectflag, ref num);
                        if (connectflag[0] != 1)
                        {
                            OpenCanFile.Text = $"连接中。。。{i}";
                            System.Threading.Thread.Sleep(1000);
                        }
                        else
                        {
                            OpenCanFile.Text = "连接成功";
                            OpenCanFile.Enabled = false;
                            MessageBox.Show("总线连接成功");
                            return;
                        }
                    }
                    OpenCanFile.Text = "打开并连接";
                    OpenCanFile.Enabled = true;
                    MessageBox.Show("总线连接失败");

                }
                #endregion
            }
        }





        /// <summary>
        /// 持续运动
        /// </summary>
        /// <param name="iaxis">轴号</param>
        /// <param name="flag">1：正向，-1：负向</param>
        public void JOGSport(int[] axisList, int flag)
        {
                PMC_Base(Data.handle, 1, axisList);
                PMC_Single_Vmove(Data.handle, axisList[0], flag);           //连续运动：1正向，-1负向                
        }

        //按下反转
        private void JogReduce_MouseDown(object sender, MouseEventArgs e)
        {
            axislist[0] =Convert.ToInt32(num_AxisNum.Value);
            PMC_Base(Data.handle, 1, axislist);
            PMC_SetUnits(Data.handle, axislist[0], 1000);
            PMC_SetSpeed(Data.handle, axislist[0], Convert.ToUInt32(num_Speed.Value));
            PMC_SetAccel(Data.handle, axislist[0], Convert.ToInt32(num_accel.Value));
            PMC_SetDecel(Data.handle, axislist[0], Convert.ToInt32(num_deccel.Value));
            JOGSport(axislist, -1);
        }

        private void JogReduce_MouseUp(object sender, MouseEventArgs e)
        {
            PMC_Single_Cancel(Data.handle, axislist[0], 3);
        }

        //正转
        private void JogAdd_MouseDown(object sender, MouseEventArgs e)
        {
            axislist[0] = Convert.ToInt32(num_AxisNum.Value);
            PMC_Base(Data.handle, 1, axislist);
            PMC_SetUnits(Data.handle, axislist[0], 1000);
            PMC_SetSpeed(Data.handle, axislist[0], Convert.ToUInt32(num_Speed.Value));
            PMC_SetAccel(Data.handle, axislist[0], Convert.ToInt32(num_accel.Value));
            PMC_SetDecel(Data.handle, axislist[0], Convert.ToInt32(num_deccel.Value));
            JOGSport(axislist, 1);
        }

        private void JogAdd_MouseUp(object sender, MouseEventArgs e)
        {
            PMC_Single_Cancel(Data.handle, axislist[0], 3);
        }


    }
}
